//
//  XXNavigationController.swift
//  APP
//
//  Created by Rdxer on 2017/12/9.
//  Copyright © 2017年 Rdxer. All rights reserved.
//

import UIKit
import Async

class XXNavigationController: XXBaseNavigationController {

    override func viewDidLoad() {
        super.viewDidLoad()
        prepareGestureRecognizerDelegate()
        navigationBar.titleTextAttributes = [
            NSAttributedStringKey.foregroundColor:AppColor.textColor
        ]
    }

    override func pushViewController(_ viewController: UIViewController, animated: Bool) {

        if self.viewControllers.count > 0 {
            let image =  #imageLiteral(resourceName: "nav_bar_back_icon")
            let barButton = UIBarButtonItem.init(image: image, style: .done, target: self, action: #selector(backButtonClick))
            viewController.navigationItem.leftBarButtonItem = barButton
            viewController.hidesBottomBarWhenPushed = true
        }

        super.pushViewController(viewController, animated: animated)
    }
    
    @objc func backButtonClick(){
        self.popViewController(animated: true)
    }
}

/// 左侧滑返回手势
extension XXNavigationController:UIGestureRecognizerDelegate{
    
    func prepareGestureRecognizerDelegate(){
        self.interactivePopGestureRecognizer?.delegate = self
    }
    
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        if self.viewControllers.count > 1 {
            return true
        }
        return false
    }
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        return true
    }
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldBeRequiredToFailBy otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        return gestureRecognizer.isKind(of: UIScreenEdgePanGestureRecognizer.self)
    }
}

